// 深度响应

const depsMap = new Map();
function track(key) {
  let dep = depsMap.get(key);
  if (!dep) {
    dep = new Set();
    depsMap.set(key, dep);
  }
  dep.add(effect);
}
function trigger(key) {
  let dep = depsMap.get(key);
  if (dep) {
    dep.forEach((effect) => {
      effect();
    });
  }
}

let product = { price: 5, quantity: 2 };
let total = 0;
let effect = () => {
  total = product.price * product.quantity;
};
track('quantity');
effect();
console.log(total); // 10

// update
product.quantity = 5;
trigger('quantity');
console.log(total); // 25
